package com.example.mydemo.threadPool;

import java.util.concurrent.*;

/**
 * 优先级队列
 */
public class PriorityBlockingQueueDemo {
    final static BlockingQueue blockingQueue = new PriorityBlockingQueue();

    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 5, TimeUnit.SECONDS,
                blockingQueue, Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        for (int i = 1; i <= 10; i++) {
            executor.execute(new Task(i));
        }
    }

    public static class Task implements Runnable, Comparable<Runnable> {
        private int i;

        public int getI() {
            return i;
        }

        public void setI(int i) {
            this.i = i;
        }

        Task(int i) {
            this.i = i;
        }

        @Override
        public int compareTo(Runnable o) {
            if (((Task) o).getI() > i) {
                return 1;
            } else {
                return -1;
            }
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+"=="+i);
        }
    }
}
